<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class User extends Controller{
    
    public function showuser(){
        $data=Db::name("user")->select();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function getone(){
        $user_username=input("user_username");
        $data=Db::name("user")->where("user_username",$user_username)->find();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function register(){
        $data=input();
        if(Db::name("user")->where("user_username",$data['user_username'])->count()>=1)
            return ['status'=>0,'message'=>'用户名已被注册'];
        $data['user_password']=md5($data['user_password']);
        if(Db::name("user")->insert($data))
            return ['status'=>1,'message'=>'注册成功'];
        else
            return ['status'=>0,'message'=>'注册失败'];
    }

    public function login(){
        $user_username=input('user_username');
        $user_password=md5(input('user_password'));
        $data_password=Db::name('user')->where("user_username",$user_username)->value("user_password");
        if($user_password==$data_password)
            return ['status'=>1,'message'=>'登录成功','user_username'=>$user_username];
        else
            return ['status'=>0,'message'=>'密码错误或者用户不存在'];
    }

    public function updateuser(){
        $user_id=input("id");
        $column=input("column");
        $value=input("value");
        if(Db::name('user')->where("user_id",$user_id)->setField($column,$value))
            return ['status'=>1,'message'=>'操作成功'];
        else
            return ['status'=>0,'message'=>'操作失败'];
    }

    public function updateByForm(){
        $data=input();
        unset($data['user_idcard_front1']);
        $user_username=$data['user_username'];
        unset($data['user_username']);
        if(Db::name("user")->where("user_username",$user_username)->update($data))
            return ['status'=>1,'message'=>'注册成功'];
        else
            return ['status'=>0,'message'=>'注册失败'];

    }
}